package jsyf.oa.service.doc;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSON;
import jsyf.oa.dao.doc.AttachHistoryDao;
import jsyf.oa.entity.doc.AttachHistoryEntity;
import jsyf.oa.facade.doc.AttachHistoryFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by Maqiuqiu on 2018/4/25.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class AttachHistoryService implements AttachHistoryFacade{

    @Autowired
    private AttachHistoryDao attachHistoryDao;

    @Override
    public String save(String jsonData) {
        AttachHistoryEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), AttachHistoryEntity.class);
        Integer rows;
        if ("0".equals(entity.getId()) || StringUtils.isBlank(entity.getId())) {
            rows = attachHistoryDao.add(entity);
        } else {
            AttachHistoryEntity attachHistoryEntity = (AttachHistoryEntity) attachHistoryDao.getIBaseEntityById(entity.getId());
            attachHistoryEntity.setMainId(entity.getMainId());
            attachHistoryEntity.setAttachName(entity.getAttachName());
            attachHistoryEntity.setAttachUrl(entity.getAttachUrl());
            attachHistoryEntity.setImageName(entity.getImageName());
            attachHistoryEntity.setImageUrl(entity.getImageUrl());
            attachHistoryEntity.setCreateUserId(entity.getCreateUserId());
            attachHistoryEntity.setCreateTime(entity.getCreateTime());
            rows = attachHistoryDao.update(attachHistoryEntity);
        }
        return rows == 0 ? "" : entity.getId();
    }

    @Override
    public Boolean delById(String id) {
        return attachHistoryDao.delById(id) > 0;
    }
}
